"""
Author  :lee
Date    :2023/11/2 18:19
Desc    :
"""
import requests
from datetime import datetime
from task.models import UserInformation
from apscheduler.schedulers.background import BackgroundScheduler


url = "https://bridge.aliyun.com/abs/ambUser/queryRelationUserList?current=1&pageSize=100&startDs=20231101&endDs=20231130"
CK = "cna=y/3DHfPTgXwCAbbwSQl6iDxS; _samesite_flag_=true; cookie2=1fd1a376b8d3a472b3d8ec42ab73f275; munb=2206199420735; _tb_token_=39be0540e13ea; login_aliyunid_pk=1198408262891689; login_current_pk=1198408262891689; aliyun_lang=zh; JSESSIONID=337BB53317B30E51E382481D59390A11; login_disaster=master; csg=442ec1c2; t=5cb75ef44da601928336f8ec8e8fdba7; login_aliyunid_ticket=*jab0vq1Nqx5N9tAstrzo_srpof_BNTwUhTOoNC1ZBeeMfKJzxdnb95hYssNIZor6q7SCxRtgmGCbifG2Cd4ZWazmBdHI6sgXZqg4XFWQfyKpeu*0vCmV8s*MT5tJl3_1$$wun2gv3SD7RuibhueH9D60; login_aliyunid_csrf=_csrf_tk_1344598933620367; hssid=CN-SPLIT-ARCEByIOc2Vzc2lvbl90aWNrZXQyAQE46PfWgrkxQAFKED23RUjvwVBJMI44SjX8Y2i1BCPVjIc66rhnt9e6e3wtfqbSlw; hsite=6; aliyun_country=CN; aliyun_site=CN; login_aliyunid_pks=BG+FphlpFIOFUmGKiop3cDpSrusdbxjlJDQZUKjy+eWBvI=; login_aliyunid=lx10****81458; tfstk=ddm9ln0S7eNYDGstU1LHgx0llp9HtcHwRfk5mSVGlXhKFY5glFlDDnMotrigjAcKkYldj5_gIqHjixjmSrqshiHx354D7jmLMbchmV4mhtejtXGYudP0GShmhVvHq3DZQo-jkQxkqHK40o1AtBJoQAr4cHo_7pMNDmONIT5dNLW6xMmnmRTDFWBLDsiQBPFRmWIADK2TW7M_9QO5QOmo2GilZ7ec5ppduZz_Lm8gHH5..; l=fBPaJ-lnPjcgWfJ3XOfwPurza77OSIRAguPzaNbMi9fP_rCp5g7PW1FDXUL9C3GVF6zvR3lc-MvXBeYB4Ijh0GKyqfl7SPDmnjvdvmf..; isg=BHt7DUDBmsHSbaY_KzwmtnYPCl7l0I_S91jfOG04V3qRzJuu9aAfIpkK5myCbOfK"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/000000000 Safari/537.36",
    "Origin": "https://promotion.aliyun.com",
    "Cookie": CK
}


scheduler = BackgroundScheduler()

@scheduler.scheduled_job('interval', seconds=30)
def ali_get():
    res = requests.get(url, headers=headers)
    data = res.json()
    print(data)
    if data and data['data']:
        data = data['data']['list']
        for item in data:
            user = {
                "user_nick": item["userNick"],
                "ali_id": item["id"],
                "aliyun_pk": item["aliyunPk"],
                "real_name_type": item["realNameType"],
                "user_type": item["userType"],
                "init_user_type": item["initUserType"],
                # "gmt_start": datetime.strptime(str(item["gmtStart"]//1000), "%Y-%m-%dT%H:%M:%S"),
                "gmt_start": datetime.fromtimestamp(item["gmtStart"] // 1000).strftime('%Y-%m-%dT%H:%M:%S'),
                # "gmt_end": datetime.strptime(str(item["gmtEnd"]//1000), "%Y-%m-%dT%H:%M:%S"),
                "gmt_end": datetime.fromtimestamp(item["gmtEnd"] // 1000).strftime('%Y-%m-%dT%H:%M:%S'),
                "total_pay_amount": item["totalPayAmount"]
            }
            try:
                old = UserInformation.objects.filter(ali_id=item["userNick"])
                if old:
                    old.update(**user)
                else:
                    UserInformation.objects.create(**user)
            except Exception as e:
                print('error:', str(e))
    else:
        print('数据为空')


